﻿using Shop.RemoteModel.AttrTemplate;
using Shop.RemoteModel.AttrTemplate.Model;
using Shop.RemoteModel.GoodsSku.Model;
using Shop.RemoteModel.SpecGroupTemplate;
using Shop.StoreGatewayModular.Interface;
using WeDonekRpc.Helper;

namespace Shop.StoreGatewayModular.Service
{
    internal class AttrTemplateService : IAttrTemplateService
    {
        public long Add (AttrTemplateAdd add)
        {
            return new AddAttrTemplate
            {
                Datum = add
            }.Send();
        }
        public void Delete (long id)
        {
            new DeleteAttrTemplate
            {
                Id = id
            }.Send();
        }
        public void Enable (long id)
        {
            new EnableAttrTemplate
            {
                Id = id
            }.Send();
        }
        public void Stop (long id)
        {
            new StopAttrTemplate
            {
                Id = id
            }.Send();
        }
        public bool Set (long id, AttrTemplateSet datum)
        {
            return new SetAttrTemplate
            {
                Id = id,
                Datum = datum
            }.Send();
        }
        public GoodsAttr[] GetAttrTrees (long categoryId)
        {
            AttrTemplateTree[] trees = new GetAttrTemplateTree
            {
                CategoryId = categoryId,
                IsEnable = true
            }.Send();
            return trees.ConvertAll(c => new GoodsAttr
            {
                Name = c.Name,
                Value = c.DefValue,
                IsDir = c.IsDir,
                Attrs = this._Format(c.Children)
            });
        }
        private GoodsAttr[] _Format (AttrTemplateTree[] datas)
        {
            if (datas.IsNull())
            {
                return null;
            }
            return datas.ConvertAll(c => new GoodsAttr
            {
                Name = c.Name,
                Value = c.DefValue,
                IsDir = c.IsDir,
                Attrs = this._Format(c.Children)
            });
        }
        public AttrTemplateTree[] GetTrees (long categoryId, bool? isEnable)
        {
            return new GetAttrTemplateTree
            {
                CategoryId = categoryId,
                IsEnable = isEnable
            }.Send();
        }
        public AttrTemplate Get (long id)
        {
            return new GetAttrTemplate
            {
                Id = id
            }.Send();
        }

        public Dictionary<long, int> SetSort (long id, int value)
        {
            return new SetAttrTemplateSort
            {
                Id = id,
                Sort = value
            }.Send();
        }
    }
}
